/**
 * 扣除奖励
 */
const addRefundReward = async function(data) {
	let res = { code: 0, msg: "" };
	let {
		taskInfo, //  任务信息
		orderInfo, // 订单信息
		userInfo, // 下单人信息
		mchInfo, // 商家配置信息
		usersRewardList, // 用户奖励列表
		refundInfo,
	} = data;
	let { order_no } = orderInfo;

	let time = Date.now();
	// 开启事务
	const transaction = await vk.baseDao.startTransaction();
	try {
		// 扣除奖励开始-----------------------------------------------------------
		for (let i = 0; i < usersRewardList.length; i++) {
			let usersRewardItem = usersRewardList[i];

			if (usersRewardItem.is_freeze) {
				// 扣除待提现余额 和 收支记录
				await vk.daoCenter.moneyDao.updateAccountWaitRewardForTransaction({
					db: transaction,
					user_id: usersRewardItem.user_id,
					value: usersRewardItem.reward_value,
					isAdd: false,
					updateTotal: true,
					checkBalance: false,
					no: order_no,
					title: `奖励扣除，因客户：【${userInfo.nickname}】退款`,
					comment: ``,
					add_type: 4, // 1 人工录入 2 用户线上充值 3 消费 4 退款 5 转账 6 余额转换 7 奖励发放 8 解锁 9 提现 10 提现拒绝后返还
					reward_type: usersRewardItem.reward_type,
					reward_type_lv: usersRewardItem.reward_type_lv,
					goods_reward_list: usersRewardItem.goods_reward_list,
					calc_rule: usersRewardItem.calc_rule,
					is_freeze: usersRewardItem.is_freeze,
					unfreeze_time: usersRewardItem.unfreeze_time,
				});
			} else {
				// 扣除可提现余额 和 收支记录
				await vk.daoCenter.moneyDao.updateAccountRewardForTransaction({
					db: transaction,
					user_id: usersRewardItem.user_id,
					value: usersRewardItem.reward_value,
					isAdd: false,
					updateTotal: true,
					checkBalance: false,
					no: order_no,
					title: `奖励扣除，因客户：【${userInfo.nickname}】退款`,
					comment: ``,
					add_type: 4, // 1 人工录入 2 用户线上充值 3 消费 4 退款 5 转账 6 余额转换 7 奖励发放 8 解锁 9 提现 10 提现拒绝后返还
					reward_type: usersRewardItem.reward_type,
					reward_type_lv: usersRewardItem.reward_type_lv,
					goods_reward_list: usersRewardItem.goods_reward_list,
				});
			}
			// 修改原奖励记录中的goods_reward_list(标记已扣除的奖励金额,用于多次退款时，计算最后一次退款扣除的奖励金额)
			await vk.daoCenter.rewardRecDao.updateGoodsRewardList({
				db: transaction,
				reward_id: usersRewardItem.reward_id,
				goods_reward_list: usersRewardItem.original_goods_reward_list,
			});

		}
		// 扣除奖励结束-----------------------------------------------------------

		// 扣除商品业绩
		await vk.daoCenter.userGoodsPerDao.addByRefundOrder({
			db: transaction,
			refundInfo,
			userInfo,
			orderInfo
		});

		// 如果已全额退款，则标记订单不结算
		if ([-3,-4].indexOf(orderInfo.status) > -1) {
			await vk.daoCenter.orderDao.updateById({
				db: transaction,
				id: orderInfo._id,
				dataJson: {
					"fx.settlement_status": 2
				}
			});
		}

		// 标记任务执行完毕
		await vk.daoCenter.orderTaskDao.updateStatusSuccess({
			db: transaction,
			taskInfo
		});

		// 为了方便测试 - 事务回滚
		//await transaction.rollback();

		// 提交事务
		await transaction.commit();
		console.log(`transaction succeeded`);
		res.msg = "ok";
		res.code = 0;
		res.taskInfo = taskInfo;
	} catch (err) {
		// 事务回滚
		return await vk.baseDao.rollbackTransaction({
			db: transaction,
			err
		});
	}

	return res;
};

module.exports = addRefundReward;
